import router from "@/router";
import Index from "@/views/Index";

// 注意：这个文件是设置动态路由的
// permissions是一个资源的数组
export function activeRouter() {
    const menusStr = sessionStorage.getItem("manage-menus");
    if (menusStr) {
        const menus = JSON.parse(menusStr)
        let root = {
            path: '/',
            name: 'Layout',
            component: Index,
            redirect: "/home",
            children: []
        }
        menus.forEach(p => renderRouter(root, p))
        if (router) {
            router.addRoute(root)
        }
    }
}

function renderRouter(root, parent) {
    if (parent.children.length > 0) {
        parent.children.forEach(child => {
            renderRouter(root, child)
        })
    } else {
        let obj = {
            path: parent.path.startsWith('/') ? parent.path : '/' + parent.path,
            name: parent.name,
            component: () => import("@/views/" + parent.pagePath + ".vue")
        };
        root.children.push(obj)
    }
}


